﻿Public Class verificacion
    Public neg As Boolean = False
    Public Sub limpiar()
        For i As Integer = 1 To 5
            'ui, vi son labels ocultos que almacenan el mayor costo de la fila o columna en el metodo de russell
            For Each Control As Control In grupbox1.Controls
                If Control.Name = "u" & i Then
                    Control.Text = ""
                ElseIf Control.Name = "v" & i Then
                    Control.Text = ""
                End If
            Next
        Next
        neg = False
    End Sub

    Public Sub revisar()
        For i = 1 To grupbox1.empresas
            For j As Integer = 1 To grupbox1.bodegas
                If grupbox1.matriz(i, j).verificacion < 0 Then
                    neg = True
                    Exit Sub
                End If
            Next
        Next
    End Sub
    Public Sub verificar()
        Dim i As Integer

        'contar las asignaciones para cada bodega
        For i = 1 To grupbox1.empresas
            For j As Integer = 1 To grupbox1.bodegas
                If grupbox1.matriz(i, j).asignacion <> 0 Then
                    For Each Control As Control In grupbox1.Controls
                        If Control.Name = "u" & i Then
                            Control.Text = Val(Control.Text) + 1
                        End If
                    Next
                End If
            Next
        Next

        'buscar el ui que tenga el valor mas alto y dejarlo en 0
        Dim aux As Integer = 0
        Dim max As Integer = 0

        For i = 1 To grupbox1.empresas
            For Each Control As Control In grupbox1.Controls
                If Control.Name = "u" & i Then
                    If max < Val(Control.Text) Then
                        max = Val(Control.Text)
                        aux = i
                    End If
                End If
            Next
        Next
        i = aux
        limpiar()
        For Each Control As Control In grupbox1.Controls
            If Control.Name = "u" & i Then
                Control.Text = 0
            End If
        Next

        'buscar los vj que junto a ui tengan asignacion y dejarlos en costoij
        For j = 1 To grupbox1.bodegas
            If grupbox1.matriz(i, j).asignacion <> 0 Then
                For Each Control As Control In grupbox1.Controls
                    If Control.Name = "v" & j Then
                        Control.Text = grupbox1.matriz(i, j).costo
                    End If
                Next
            End If
        Next
        'calcular el resto de los ui y vj
        For k = 1 To 5
            For i = 1 To grupbox1.empresas
                For j As Integer = 1 To grupbox1.bodegas
                    If grupbox1.matriz(i, j).asignacion <> 0 Then
                        For Each u As Control In grupbox1.Controls
                            If u.Name = "u" & i Then
                                For Each v As Control In grupbox1.Controls
                                    If v.Name = "v" & j Then
                                        If u.Text <> "" And v.Text = "" Then
                                            v.Text = grupbox1.matriz(i, j).costo - Val(u.Text)
                                        End If
                                        If v.Text <> "" And u.Text = "" Then
                                            u.Text = grupbox1.matriz(i, j).costo - Val(v.Text)
                                        End If
                                    End If
                                Next
                            End If
                        Next
                    End If
                Next
            Next
        Next
        'recorrer matriz y en matriz(i,j).verificacion asignar matriz(i,j).costo-ui-vj
        For i = 1 To grupbox1.empresas
            For j As Integer = 1 To grupbox1.bodegas
                For Each u As Control In grupbox1.Controls
                    If u.Name = "u" & i Then
                        For Each v As Control In grupbox1.Controls
                            If v.Name = "v" & j Then
                                grupbox1.matriz(i, j).verificacion = grupbox1.matriz(i, j).costo - Val(u.Text) - Val(v.Text)
                            End If
                        Next
                    End If
                Next
            Next
        Next
        'cargarmatriz.verificacion en panel.item(0)
        For i = 1 To grupbox1.empresas
            For j As Integer = 1 To grupbox1.bodegas
                For Each control As Control In grupbox1.splitContainer1.Panel1.Controls
                    If TypeOf control Is Panel Then
                        If control.Name = "Panel" & i & j Then
                            control.Controls.Item(0).Text = grupbox1.matriz(i, j).verificacion
                            control.Controls.Item(0).Enabled = True
                            control.Controls.Item(0).Visible = True
                        End If
                    End If
                Next
            Next
        Next
    End Sub
End Class
